﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DataAccess;
using QuanLyKhachSan.TemplateReport;
using DevExpress.XtraReports.UI;
using NLog;

namespace QuanLyKhachSan
{
    public partial class TabKhachHang : XtraUserControl, IGeneatetabName
    {
        KhachHangManager controller = new KhachHangManager();
        BindingSource bs = new BindingSource();
        protected static Logger _logger = LogManager.GetCurrentClassLogger();

        public TabKhachHang()
        {
            InitializeComponent();
        }

        public string Getname()
        {
            return "Khách hàng";
        }
        private void TabKhachHang_Load(object sender, EventArgs e)
        {
            Loading();
        }

        private void Loading()
        {
            if (!controller.LayDanhSach().Result.Any())
            {
                MessageBox.Show(controller.LayDanhSach().Exceptions.First().Message);
            }
            else
            {
                bs.DataSource = controller.LayDanhSach().Result;
                dgvKhachHang.DataSource = bs;
                for (int i = 7; i < dgvKhachHang.Columns.Count; i++)
                {
                    dgvKhachHang.Columns[i].Visible = false;
                }
            }
        }

        private void btnThem_Click(object sender, EventArgs e)
        {

            bool them = true;
            _logger.Info(Commons.ToString() + ", Chọn thao tác thêm khách hàng");

            frmThongTinKhachHang frm = new frmThongTinKhachHang(them);
            frm.ShowDialog();
            KhachHang kh = frm.kh;
            if (kh != null && frm.themthanhcong)
            {
                if (controller.LayDanhSach().Result.Count <= 1)
                {
                    Loading();
                }
                else
                {
                    bs.Add(kh);
                    dgvKhachHang.Rows[TimDong(frm.kh.MaKH)].Selected = true;
                }
            }
        }


        private void btnXoa_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ", Chọn thao tác xóa khách hàng");

            DialogResult kq = MessageBox.Show("Bạn có muốn xóa dữ liệu này không?", "CẢNH BÁO", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
            if (kq == DialogResult.Yes)
            {
                ActionResult aResult = controller.Xoa(toolStripStatusLabel2.Text);
                if (!aResult.Exceptions.Any())
                {
                    MessageBox.Show("Xóa thành công khách hàng ID= " + toolStripStatusLabel2.Text);
                    bs.RemoveAt(TimDong(toolStripStatusLabel2.Text));
                    dgvKhachHang.Rows[0].Selected = true;
                    _logger.Info(Commons.ToString() + ", Xóa thành công khách hàng ID= " + toolStripStatusLabel2.Text);
                }
                else
                {
                    string s = "";
                    foreach (var ex in aResult.Exceptions)
                    {
                        s += ex.Message + "\n";
                    }
                    MessageBox.Show(s);
                    _logger.Error(s);
                    MessageBox.Show("Xóa thành công khách hàng ID= " + toolStripStatusLabel2.Text);
                    bs.RemoveAt(TimDong(toolStripStatusLabel2.Text));
                    dgvKhachHang.Rows[0].Selected = true;
                    _logger.Info(Commons.ToString() + ", Xóa thành công khách hàng ID= " + toolStripStatusLabel2.Text);}
            }
        }

        private void dgvKhachHang_Click(object sender, EventArgs e)
        {
            int selectedRow = TimDong();
            if (selectedRow >= 0)
            {
                toolStripStatusLabel2.Text = dgvKhachHang.Rows[selectedRow].Cells[0].Value + "";
            }
        }

        private int TimDong()
        {
            for (int i = 0; i < dgvKhachHang.Rows.Count; i++)
            {
                if (dgvKhachHang.Rows[i].Selected)
                {
                    return i;
                }
            }
            return -1;
        }

        private int TimDong(string ma)
        {
            for (int i = 0; i < dgvKhachHang.Rows.Count; i++)
            {
                if (dgvKhachHang.Rows[i].Cells[0].Value.ToString() == ma)
                {
                    return i;
                }
            }
            return -1;
        }

        private void btnCapNhat_Click(object sender, EventArgs e)
        {

            if (toolStripStatusLabel2.Text != "" && controller.LayDanhSach().Result.Count > 0)
            {
                _logger.Info(Commons.ToString() + ", Chọn thao tác cập nhật khách hàng");

                KhachHang khold = (KhachHang)bs[TimDong(toolStripStatusLabel2.Text)];
                frmThongTinKhachHang frm = new frmThongTinKhachHang(khold);
                frm.ShowDialog();
                KhachHang khnew = frm.kh;
                if (khnew != null && frm.capnhatthanhcong)
                {
                    bs.RemoveAt(TimDong(toolStripStatusLabel2.Text));
                    bs.Add(khnew);
                    dgvKhachHang.Rows[TimDong(frm.kh.MaKH)].Selected = true;
                }
            }
        }

        private void btnTimKiem_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ", Chọn thao tác tìm kiếm khách hàng");

            frmTimKiemKhachHang frm = new frmTimKiemKhachHang();
            frm.ShowDialog();
            if (frm.thongtin != "" || frm.rdName != "")
            {
                switch (frm.rdName)
                {
                    case "rdFindByName":
                        ActionResultAsList<KhachHang> aResult1 = controller.TimKiemTheoDK(kh => kh.HoTenKH.Contains(frm.thongtin));
                        if (!aResult1.Exceptions.Any())
                        {
                            bs.DataSource = aResult1.Result;
                        }
                        else
                        {
                            string s = "";
                            foreach (var ex in aResult1.Exceptions)
                            {
                                s += ex.Message + "\n";
                            }
                            MessageBox.Show(s);
                            _logger.Error(s);
                        }
                        break;
                    case "rdFindByNation":
                        ActionResultAsList<KhachHang> aResult2 = controller.TimKiemTheoDK(kh => kh.MaQT == frm.thongtin);
                        if (!aResult2.Exceptions.Any())
                        {
                            bs.DataSource = aResult2.Result;
                        }
                        else
                        {
                            string s = "";
                            foreach (var ex in aResult2.Exceptions)
                            {
                                s += ex.Message + "\n";
                            }
                            MessageBox.Show(s);
                            _logger.Error(s);
                        }
                        break;
                    case "rdFindAll":
                        bs.DataSource = controller.LayDanhSach().Result;
                        break;
                }
            }
        }

        private void ibtnIn_Click(object sender, EventArgs e)
        {
            btnIn.PerformClick();
        }

        private void btnIn_Click(object sender, EventArgs e)
        {
            _logger.Info(Commons.ToString() + ", Chọn thao tác in danh sách khách hàng");

            XrptKhachHang rptKhachHang = new XrptKhachHang();
            rptKhachHang.CreateDocument();
            rptKhachHang.ShowPreview();
        }

        private void ibtnThem_Click(object sender, EventArgs e)
        {
            btnThem.PerformClick();
        }

        private void ibtnXoa_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void ibtnCapNhat_Click(object sender, EventArgs e)
        {
            btnCapNhat.PerformClick();
        }

        private void ibtnTimKiem_Click(object sender, EventArgs e)
        {
            btnTimKiem.PerformClick();
        }

        private void themKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnThem.PerformClick();
        }

        private void capnhatKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnCapNhat.PerformClick();
        }

        private void xoaKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnXoa.PerformClick();
        }

        private void timkiemKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnTimKiem.PerformClick();
        }

        private void indsKHToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnIn.PerformClick();
        }
    }
}
